package com.zy.experience_system.service.impl;

import com.zy.experience_system.entity.*;
import com.zy.experience_system.enums.ResultEnum;
import com.zy.experience_system.exception.CommonException;
import com.zy.experience_system.form.ClassAndSetForm;
import com.zy.experience_system.form.ClassInformationForm;
import com.zy.experience_system.repository.*;
import com.zy.experience_system.service.ClassService;
import com.zy.experience_system.vo.ClassAdminVO;
import com.zy.experience_system.vo.ClassCountVO;
import com.zy.experience_system.vo.StuInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassInformationRepository classInformationRepository;
    @Autowired
    private StuAndClassRepository stuAndClassRepository;
    @Autowired
    private ClassAndSetRepository classAndSetRepository;
    @Autowired
    private StuAndSetRepository stuAndSetRepository;
    @Autowired
    private StuInformationRepository stuInformationRepository;

    @Override
    public Integer save(ClassInformationForm classInformationForm) {
        ClassInformation classInformation;
        if (classInformationForm.getClassId() == null) {
            classInformation = new ClassInformation();
            BeanUtils.copyProperties(classInformationForm, classInformation);
        } else {
            classInformation = classInformationRepository.findById(classInformationForm.getClassId()).get();
        }
        classInformation.setClassName(classInformationForm.getClassName());
        ClassInformation classInformation1 = classInformationRepository.save(classInformation);
        return (classInformation1.getClassId());
    }

    @Override
    public List<ClassCountVO> findAllByTeach(String teachId) {
        List<ClassInformation> classInformationList = classInformationRepository.findAllByTeachId(teachId);
        List<ClassCountVO> classCountVOList = new ArrayList<>();
        for (ClassInformation classInformation : classInformationList) {
            ClassCountVO classCountVO = new ClassCountVO();
            BeanUtils.copyProperties(classInformation, classCountVO);
            Integer count = stuAndClassRepository.countAllByClassId(classInformation.getClassId());
            classCountVO.setCount(count);
            classCountVOList.add(classCountVO);
        }
        return classCountVOList;
    }

    public List<ClassAdminVO> findAll() {
        List<ClassInformation> classInformationList = classInformationRepository.findAll();
        List<ClassAdminVO> classAdminVOList = new ArrayList<>();
        for (ClassInformation classInformation : classInformationList) {
            ClassAdminVO classAdminVO = new ClassAdminVO();
            BeanUtils.copyProperties(classInformation, classAdminVO);
            Integer count = stuAndClassRepository.countAllByClassId(classInformation.getClassId());
            classAdminVO.setCount(count);
            classAdminVOList.add(classAdminVO);
        }
        return classAdminVOList;
    }

    @Override
    public void delete(Integer classId) {
        if (classInformationRepository.findById(classId).isPresent()) {
            classInformationRepository.deleteByClassId(classId);
            return;
        }
        throw new CommonException(ResultEnum.FAIL);
    }

    @Override
    public void save(ClassAndSetForm classAndSetForm) {
        Integer classId = classAndSetForm.getClassId();
        Integer setId = classAndSetForm.getSetId();
        if (classId != null && setId != null) {
            if (classAndSetRepository.findByClassIdAndSetId(classId, setId) == null) {
                ClassAndSet classAndSet = new ClassAndSet();
                BeanUtils.copyProperties(classAndSetForm, classAndSet);
                classAndSetRepository.save(classAndSet);
                List<StuAndClass> stuAndClassList = stuAndClassRepository.findAllByClassId(classId);
                for (StuAndClass stuAndClass : stuAndClassList) {
                    String stuId = stuAndClass.getStuId();
                    if (stuAndSetRepository.findByStuIdAndSetId(stuId, setId) == null) {
                        StuAndSet stuAndSet = new StuAndSet();
                        stuAndSet.setSetId(setId);
                        stuAndSet.setStuId(stuId);
                        stuAndSetRepository.save(stuAndSet);
                    }
                }
                return;
            }
            throw new CommonException(ResultEnum.FAIL);
        }
        throw new CommonException(ResultEnum.FAIL);
    }

    @Override
    public void delete(ClassAndSetForm classAndSetForm) {
        Integer classId = classAndSetForm.getClassId();
        Integer setId = classAndSetForm.getSetId();
        if (classId != null && setId != null) {
            if (classAndSetRepository.findByClassIdAndSetId(classId, setId) != null) {
                classAndSetRepository.deleteByClassIdAndSetId(classId, setId);
                List<StuAndClass> stuAndClassList = stuAndClassRepository.findAllByClassId(classId);
                for (StuAndClass stuAndClass : stuAndClassList) {
                    String stuId = stuAndClass.getStuId();
                    stuAndSetRepository.deleteBySetIdAndStuId(setId, stuId);
                }
                return;
            }
            throw new CommonException(ResultEnum.FAIL);
        }
        throw new CommonException(ResultEnum.FAIL);

    }

    @Override
    public List<ClassCountVO> findAllBySet(Integer setId) {
        List<ClassAndSet> classAndSetList = classAndSetRepository.findAllBySetId(setId);
        if (classAndSetList != null) {
            List<ClassCountVO> classCountVOList = new ArrayList<>();
            for (ClassAndSet classAndSet : classAndSetList) {
                Integer classId = classAndSet.getClassId();
                ClassInformation classInformation = classInformationRepository.findById(classId).get();
                ClassCountVO classCountVO = new ClassCountVO();
                BeanUtils.copyProperties(classInformation, classCountVO);
                Integer count = stuAndClassRepository.countAllByClassId(classInformation.getClassId());
                classCountVO.setCount(count);
                classCountVOList.add(classCountVO);
            }
            return classCountVOList;
        }
        throw new CommonException(ResultEnum.FAIL);
    }


}
